package cn.dengta.webapp.user.entity;

import java.util.Date;

import cn.dengta.common.model.*;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.entity.moment.*;
import cn.dengta.webapp.user.vo.LinkVO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.X;

/**
 * 动态
 *
 * @author lgy
 * @since 2022-03-02
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName(value = "dt_moment", autoResultMap = true)
public class Moment extends BizEntity implements BizFlag.WritableBizFlag {

	/** 相册最大上传数 */
	public static final int PHOTO_UPLOAD_MAX = 9;
	/** 视频最大上传数 */
	public static final int VIDEO_UPLOAD_MAX = 1;
	/** 内容最大上传数 */
	public static final int CONTENT_UPLOAD_MAX = 800;
	/** 标识：审核复制标记 */
	public static final long BIZ_FLAG_AUDIT_COPY_MARK = 1L;

	/** 用户ID */
	private Long userId;
	/** 内容 */
	private String content;
	/** 图片（多张图片以逗号隔开） */
	private String images;
	/** 视频 */
	private String videos;
	/** 点赞数 */
	private Integer digCount;
	/** 打赏数 */
	private Integer donateCount;
	/** 打赏金额 */
	private Integer donateAmount;
	/** 是否启用评论功能：1=启用；0=停用；-1=禁止 */
	private Integer commentStatus;
	/** 评论数 */
	private Integer commentCount;
	/** 评论点赞数 */
	private Integer commentDigCount;
	/** 分享数 */
	private Integer shareCount;
	/** 标签ID */
	private Long tagId;
	/** 业务标识 */
	private long bizFlag;
	/** 马甲包 */
	protected WesKit wesKit;
	/** 扩展信息 */
	@TableField(value = EXT, typeHandler = FastjsonTypeHandler.class)
	private MomentExt ext;

	private transient LinkVO linkVO;

	public Moment init(Moment input, Date now) {
		this.userId = X.expectNotNull(userId, input.userId);
		this.content = X.expectNotNull(input.content, "");
		this.images = X.expectNotNull(input.images, "");
		this.videos = X.expectNotNull(input.videos, "");
		final Integer zero = 0;
		this.digCount = X.expectNotNull(digCount, zero);
		this.donateCount = X.expectNotNull(donateCount, zero);
		this.donateAmount = X.expectNotNull(donateAmount, zero);
		this.commentStatus = X.expectNotNull(input.commentStatus, commentStatus, 1);
		this.commentCount = X.expectNotNull(commentCount, zero);
		this.commentDigCount = X.expectNotNull(commentDigCount, zero);
		this.tagId = input.tagId;
		this.shareCount = X.expectNotNull(shareCount, zero);
		this.bizFlag = X.expectNotNull(bizFlag, 0L);
		super.initTime(now);
		super.init(input.status, input.state);
		return this;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public boolean canShow() {
		return getStatus() > VerifyStatus.WAIT_VERIFY.value;
	}

	public boolean canDelete() {
		return getStatus() >= VerifyStatus.WAIT_VERIFY.value;
	}

	public boolean isOpen() {
		return super.isEnabled() && canShow();
	}

	public boolean canDig() {
		return isOpen() && digCount >= 0;
	}

	public boolean hasDig() {
		return digCount > 0;
	}

	public boolean canDonate() {
		return isOpen() && donateCount >= 0;
	}

	public boolean hasDonate() {
		return donateCount > 0;
	}

	public boolean canCancelDig() {
		return isOpen() && hasDig();
	}

	/** 是否可以评论 */
	public boolean hasComment() {
		return isOpen() && Status.asResult(this.commentStatus);
	}

	public static final String USER_ID = "user_id";
	public static final String CONTENT = "content";
	public static final String IMAGES = "images";
	public static final String VIDEOS = "videos";
	public static final String DIG_COUNT = "dig_count";
	public static final String DONATE_COUNT = "donate_count";
	public static final String DONATE_AMOUNT = "donate_amount";
	public static final String TAG_ID = "tag_id";
	public static final String COMMENT_STATUS = "comment_status";
	public static final String COMMENT_COUNT = "comment_count";
	public static final String SHARE_COUNT = "share_count";
	public static final String WES_KIT = "wes_kit";
	public static final String EXT = "ext";

	@Override
	public void setBizFlag(long flag) {
		bizFlag = flag;
	}

}
